package com.jp.knowledge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jp.knowledge.R;

import org.xutils.common.util.DensityUtil;

/**
 * Created by 528787067@qq.com on 2016/12/30.
 */

public class SelectDialog extends Dialog implements View.OnClickListener {

    private View contentView;
    private LinearLayout contentLayout;
    private TextView title;
    private TextView cancelBtn;

    private Context context;
    private Object tag;

    public SelectDialog(Context context) {
        super(context);

        this.context = context;

        contentView = View.inflate(context, R.layout.select_dialog, null);
        contentLayout = (LinearLayout) contentView.findViewById(R.id.dialog_content);
        title = (TextView) contentView.findViewById(R.id.dialog_title);
        cancelBtn = (TextView) contentView.findViewById(R.id.cancle_setfont_btn);
        cancelBtn.setClickable(true);
        cancelBtn.setOnClickListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(contentView);
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.select_dialog_anim);
        window.setBackgroundDrawableResource(R.color.translate);
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.width = (int) (DensityUtil.getScreenWidth() - context.getResources().getDimension(R.dimen.space_big) * 2 + 0.5);
        window.setAttributes(wl);
    }

    public void addItem(String text){
        int itemCount = getItemCount();
        addItem(itemCount, text);
    }

    public boolean addItem(int position, String text){
        int itemCount = getItemCount();
        if(position < 0 || position > itemCount)
            return false;
        View line = new View(context);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                (int)(context.getResources().getDimension(R.dimen.line_heigth)+0.5)));
        line.setBackgroundColor(context.getResources().getColor(R.color.line_gray));

        TextView itemView = new TextView(context);
        itemView.setText(text);
        itemView.setTextColor(cancelBtn.getTextColors());
        itemView.setTextSize(TypedValue.COMPLEX_UNIT_PX, cancelBtn.getTextSize());
        itemView.setPadding(title.getPaddingLeft(),
                title.getPaddingTop(),
                title.getPaddingRight(),
                title.getPaddingBottom()
        );
        itemView.setGravity(title.getGravity());
        itemView.setLayoutParams(title.getLayoutParams());
        itemView.setBackground(context.getResources().getDrawable(R.drawable.normal_click_bg));
        itemView.setClickable(true);
        itemView.setOnClickListener(this);

        contentLayout.addView(line, 1+position*2);
        contentLayout.addView(itemView, 1+position*2+1);
        return true;
    }

    public boolean changedItemText(int position, String content){
        int itemCount = getItemCount();
        if(position < 0 || position > itemCount-1)
            return false;
        try{
            ((TextView)contentLayout.getChildAt(1+position*2+1)).setText(content);
            return true;
        } catch (Exception e){
            return false;
        }
    }

    public boolean removeItem(int position){
        int itemCount = getItemCount();
        if(position < 0 || position > itemCount-1)
            return false;
        try {
            contentLayout.removeViewAt(1+position*2);   // 移除分割线
            contentLayout.removeViewAt(1+position*2);   // 移除按钮
            return true;
        } catch (Exception e){
            return false;
        }
    }

    public int getItemCount(){
        return (contentLayout.getChildCount()+1)/2-1;
    }

    public void setTitle(String title){
        this.title.setText(title);
    }

    public void visibleTitle(boolean visible){
        title.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public void setCancelBtn(String text){
        cancelBtn.setText(text);
    }

    public void visibleCancelButton(boolean visible){
        cancelBtn.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public void setTag(Object tag){
        this.tag = tag;
    }

    public Object getTag(){
        return this.tag;
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.cancle_setfont_btn){
            if(this.cancelClickListener != null)
                this.cancelClickListener.onSelectCancelClick();
        } else if(this.itemClickListener != null){
            int childIndex = contentLayout.indexOfChild(v);
            this.itemClickListener.onSelectItemClick((childIndex+1)/2-1);
        }
        this.dismiss();
    }

    private OnSelectDialogItemClickListener itemClickListener;
    public void setOnDialogItemClickListener(OnSelectDialogItemClickListener listener){
        this.itemClickListener = listener;
    }
    public interface OnSelectDialogItemClickListener {
        void onSelectItemClick(int position);
    }

    private OnSelectDialogCancelClickListener cancelClickListener;
    public void setOnDialogCancelClickListener(OnSelectDialogCancelClickListener listener){
        this.cancelClickListener = listener;
    }
    public interface OnSelectDialogCancelClickListener{
        void onSelectCancelClick();
    }
}
